<template>
  <div>
    <!--    搜索行-->
    <el-card style="height: auto;padding:0;" class="head">
      <div id="op" style="margin-bottom: 5px">
        <el-row type="flex" justify="center">
          <el-col :span="2" style="margin-left: 30px">
            <div style="display: flex;">
              <el-button
                style="margin: 5px 10px 5px"
                type="success"
                @click="dialogFormVisible = true"
                size="mini"
                >请确认预定的信息
              </el-button>

              <el-button
                style="margin: 5px 10px 5px"
                type="info"
                size="mini"
                @click="back"
                >返回
              </el-button>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-card>

    <!-- 新增、修改时弹出的对话框  -->
    <el-dialog
      title="订单信息"
      :visible.sync="dialogFormVisible"
      style="width: 900px;"
    >
      <el-form
        prop="torderForm"
        ref="torderForm"
        :model="form"
        label-width="auto"
        style="width: 400px;"
        label-suffix=":"
        :rules="rules"
      >
        <el-form-item label="总价格"><b style="color: #409EFF;">{{ this.price }}</b> ￥ </el-form-item>
        <el-form-item label="用户名" prop="name">
          <el-input v-model="form.name"></el-input> 
        </el-form-item>
        <el-form-item label="实名认证" prop="autonym">
          <el-tag type="success" v-if="myInfo.icCode">已实名</el-tag>
          <el-tag type="danger" v-if="!myInfo.icCode">未实名</el-tag>
        </el-form-item>
        <el-form-item label="手机号码" prop="linkTel">
          <el-input v-model="form.linkTel"></el-input>
        </el-form-item>

        <el-form-item label="说明">
          <el-input v-model="form.requirement"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="saveUserInfo" type="primary">取 消</el-button>
        <el-button type="success" @click="onSubmit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import newAxios from "../../utils/axios";
export default {
  name: "hotelOrder",
  data() {
    //表单校验规则
    const rules = {
      name: [
        {
          required: true,
          message: "请输入2到 20位名字",
          trigger: "change",
        },
        {
          min: 2,
          max: 20,
          message: "长度在 2到 20 个名字",
          trigger: "blur",
        },
      ],

      linkTel: [
        {
          required: true,
          message: "请输入11位手机号码",
          trigger: "blur,change",
        },
        {
          min: 11,
          max: 11,
          message: "长度11字符",
          trigger: "blur,change",
        },
      ],
    };
    return {
      rules,
      tableData: [],
      search: "",
      form: {},
      day: 0,
      price: 0,
      // id: 0,
      setoffTime: "",
      dialogTableVisible: false,
      dialogFormVisible: false,
      formLabelWidth: "120px",
      total: 0,
      pageSize: 6,
      pageNow: 1,
      searchName: "",
      multipleSelection: [],
      myInfo:{}
    };
  },
  created() {
    this.price = parseInt(this.$route.query.price);
    this.setoffTime = this.$route.query.setoffTime;
    this.getUser();
  },
  methods: {
    back() {
      this.$router.back();
    },
    saveUserInfo() {
      this.dialogFormVisible = false;
    },
    handleEdit(index, row) {
      console.log(index, row);
      this.dialogFormVisible = true; //展示编辑表单
      this.form = row;
    },
    
    getUser() {
      newAxios
        .get("/admin/findUserById")
        .then((res) => res.data)
        .then((res) => {
          if (res.code == 200) {
            console.log(res.data);
            this.myInfo = res.data;
            this.form.name = this.myInfo.username;
            this.form.linkTel = this.myInfo.linkTel;
          }else{
            this.$message.error("请先登录噢亲！！");
          }
        })
        .catch((err) => {
         console.log(err);
        });
    },
    onSubmit() {
      this.$refs.torderForm.validate((valid) => {
        if (valid) {
          newAxios
            .post("/order/userOrder", {
              productId: parseInt(this.$route.query.car_id),
              orderType: 0,
              peopleCount: 1,
              price: this.price,
              requirement: this.form.requirement,
              setoffTime: this.setoffTime,
              linkTel: this.form.linkTel,
            })
            .then((res) => res.data)
            .then((res) => {
              console.log(res);
              if (res.code == 200) {
                this.$message({
                  message:'购票成功！',
                  type:'success'
                });
                this.dialogFormVisible = false;
                this.$router.push({
                  path: "/car",
                });
              } else {
                this.$message = "预定失败，请去留言";
              }
            });
        } else {
          this.$message({
            message: "请检查数据是否正确",
            type: "warning",
          });
        }
      });
    },

    //刷新data
    //   this.findAll();

    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(val);
    },
  },
};
</script>

<style>
.el-card {
  margin: 10px 10px 5px 10px;
}

.el-card__body {
  padding: 0;
}

.el-card .el-table {
  margin-top: 10px;
}

#op {
  margin-top: 10px;
}

.head .el-input--mini .el-input__inner {
  width: 200px;
  height: 40px;
  line-height: 40px;
}

.el-input--mini {
  width: auto;
}

label {
  width: auto;
}
</style>
